package de.nroz.control;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ArtikelUtil
{

	private final static SimpleDateFormat dfCustom = new SimpleDateFormat("ddMMyy");

	private final static DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);

	private static DecimalFormat dc = new DecimalFormat("#########0,00");

	{
	}

	public static String date2String(Date date)
	{
		if (date != null)
		{
			return df.format(date);
		}
		else
		{
			return "";
		}
	}

	public static Date string2Date(String date)
	{
		try
		{
			if (date.indexOf('.') == -1)
			{
				return dfCustom.parse(date);
			}
			else
			{
				return df.parse(date);
			}
		}
		catch (ParseException e)
		{
		}
		return null;
	}

	public static BigDecimal string2Number(String s)
	{
		try
		{
			Number n = null;
			if ((n = dc.parse(s)) instanceof Long)
			{
				return new BigDecimal(s);
			}
			return (BigDecimal) n;
		}
		catch (ParseException e)
		{
			return null;
		}
	}

	public static BigDecimal positiveNumber(BigDecimal num)
	{
		if (num != null)
		{
			return num.abs();
		}
		else
		{
			return BigDecimal.ZERO;
		}
	}

	public static BigDecimal computeGewinn(BigDecimal ekPreis, BigDecimal vkPreis)
	{
		return vkPreis.subtract(ekPreis);
	}
}
